package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.place;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.device.dao.db.Device;
import com.gzhryc.shared_device.common.device.services.DeviceService;
import com.gzhryc.shared_device.common.device.services.dto.DeviceSearch;
import com.gzhryc.shared_device.oem.code.device.EDeviceFunCode;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.h5.agent.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;

public class PlaceDeviceController {

    @Route(value = {"/place/device/list.html"})
    public String list(HttpServletRequest request) {
        Long placeId = WebUtils.getLong(request, "placeId");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeId);
            if(place != null && place.getUserId().equals(loginUser.getId())) {
                WebUtils.setAttribute(request, place);
            }
        }
        request.setAttribute("deviceFuncCodeList", EDeviceFunCode.values());
        return "/place/device/list.html";
    }

    @Route(value = "/place/device/list.data")
    public ListResult<Device> listData(HttpServletRequest request) {
        ListResult<Device> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        DeviceSearch search = WebUtils.getParameter(request, DeviceSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        if (search.getPlaceId() != null) {
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(search.getPlaceId());
                if(place != null && place.getUserId().equals(loginUser.getId())) {
                    result.getBody().setTotal(DeviceService.self().countBySearch(search));
                    result.getBody().setData(DeviceService.self().findBySearch(search, pageNo, pageLimit));
                    if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                }else{
                    result.setCode(DtoConstants.NOT_POWER,"无权限查看");
                }
            }
        }
        return result;
    }

    @Route(value = {"/place/device/add.html"})
    public String add(HttpServletRequest request) {
        Long placeId = WebUtils.getLong(request, "placeId");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeId);
            if(place != null && place.getUserId().equals(loginUser.getId())) {
                request.setAttribute("place", place);
            }
        }
        return "/place/device/edit.html";
    }

    @Route(value = {"/place/device/add.do"})
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "部署失败，请稍后再试");
        Long placeId = WebUtils.getLong(request, "placeId");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        String bindNote = WebUtils.getString(request, "bindNote");
        if (placeId != null && StringTools.isNotBlank(deviceSn)) {
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeId);
                if (place == null) {
                    result.setMsg("未找到网点信息");
                    return result;
                }

                if (place.getUserId().equals(loginUser.getId())) {
                    Device device = DeviceService.self().getByDeviceSn(deviceSn);
                    if (device == null) {
                        result.setMsg("设备不存在");
                        return result;
                    }
                    if (device.getPlaceId() != null) {
                        result.setMsg("设备已部署");
                        return result;
                    }

                    device.setPlaceId(placeId);
                    device.setPlaceName(place.getName());
                    device.setProvince(place.getProvince());
                    device.setCity(place.getCity());
                    device.setCityArea(place.getCityArea());
                    device.setAreaCode(place.getAreaCode());
                    device.setBindNote(bindNote);

                    try {
                        if (DeviceService.self().deploy(device, PlacePhysicalStoreService.self())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        }
                    } catch (LogicException e) {
                        result.setMsg(e.getMessage());
                    }
                }else{
                    result.setCode(DtoConstants.NOT_POWER,"无权限操作");
                }
            }
        }
        return result;
    }

    @Route(value = {"/place/device/bind_note/update.html"})
    public String updateBindNote(HttpServletRequest request) {
        return "/place/device/bind_note.html";
    }

    @Route(value = {"/place/device/bind_note/update.do"})
    public AjaxResult doUpdateBindNote(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改失败，请稍后再试");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        String bindNote = WebUtils.getString(request, "bindNote");
        if (StringTools.isNotBlank(deviceSn)) {
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                Device device = DeviceService.self().getByDeviceSn(deviceSn);
                if (device == null) {
                    result.setMsg("设备不存在");
                    return result;
                }
                if (device.getPlaceId() == null) {
                    result.setMsg("设备未部署");
                    return result;
                }

                PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(device.getPlaceId());
                if (place != null && place.getUserId().equals(loginUser.getId())) {
                    if (DeviceService.self().updateBindNote(deviceSn, bindNote)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                }else{
                    result.setCode(DtoConstants.NOT_POWER,"无权限操作");
                    return result;
                }
            }
        }
        return result;
    }

    @Route(value = {"/place/device/undeploy.do"})
    public AjaxResult doUndeploy(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "移除失败，请稍后再试");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        try {
            Device device = DeviceService.self().getByDeviceSn(deviceSn);
            if (device == null) {
                result.setMsg("设备不存在");
                return result;
            }
            if (device.getPlaceId() == null) {
                result.setMsg("设备未部署");
                return result;
            }

            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(device.getPlaceId());
                if(place != null && place.getUserId().equals(loginUser.getId())) {
                    if (DeviceService.self().undeploy(device, PlacePhysicalStoreService.self())) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                }else{
                    result.setCode(DtoConstants.NOT_POWER,"无权限操作");
                    return result;
                }
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
